package com.base.cn.platform.os.controller.course.mould;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.course.mould.CouMouldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台素材模块管理Controller
 *
 * @author s.li
 * @create 2018-04-23-19:58
 */
@Controller
@RequestMapping("/admin/course/mould")
public class AdminMouldController extends AdminBaseController {
    @Autowired
    private CouMouldService couMouldService;

    /**
     * 显示模块详情
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/showMouldInfo")
    public String showMouldInfo(Model model,
                                @RequestParam("id") BigDecimal id){
        Map<String,Object> mould = couMouldService.findCouMouldById(id,true,false,true);
        model.addAttribute("mould",mould);
        return resultPage("/course/mould/show-mould-info");
    }

    /**
     * 初始化模块列表页面
     * @return 操作页面
     */
    @RequestMapping("/findMouldList")
    public String findMouldList(){
        return resultPage("/course/mould/mould-list");
    }

    /**
     * 加载模块数据
     * @param model
     * @param params
     * @param currentPage
     * @return
     */
    @RequestMapping("/ajaxFindMouldList")
    public String ajaxFindMouldList(Model model,
                                    HttpServletRequest request,
                                    @RequestBody Map<String,Object> params, @RequestParam("currentPage") int currentPage){
        params.put("sysUserId",loginUtil.getLoginUserId(request));
        Map<String,Object> dataMap = couMouldService.findCouMouldPage(params,currentPage,true,true,false);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/course/mould/mould-list-ajax");
    }

    /**
     * 初始化修改/添加模块页面
     * @param model
     * @param id 模块ID，修改时传入
     * @return 操作页面
     */
    @RequestMapping(value = {"/initUpdate","/initAdd"})
    public String initUpdateOrAdd(Model model,@RequestParam(value = "id",required = false,defaultValue = "0") BigDecimal id){
        Map<String,Object> mould = null;
        if(DataUtil.idIsNotNull(id)){
            mould = couMouldService.findCouMouldById(id,false,false,false);
        }
        if(mould==null){
            mould = new HashMap<>();
        }
        model.addAttribute("mould",mould);
        return resultPage("/course/mould/save_mould");
    }

    /**
     * 保存模块数据
     * @param request HttpServletRequest
     * @param params 模块数据参数对象
     * @return Map<String,Object>
     */
    @RequestMapping("/saveMould")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.COURSE,action = "保存或修改模块数据")
    public Map<String,Object> saveMould(HttpServletRequest request,@RequestBody Map<String, Object> params){
        params.put("createUserId",loginUtil.getLoginUserId(request));
        return couMouldService.saveMould(params);
    }

    /**
     * 修改模块状态
     * @param ids 模块ID串
     * @param status 状态，1未发布，2已发布，3隐藏，4删除，5恢复
     * @return Map<String,Object>
     */
    @RequestMapping("/updateMouldStatus")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.COURSE,action = "修改模块状态")
    public Map<String,Object> updateMouldStatus(@RequestParam("ids") String ids,@RequestParam("status") int status){
        return couMouldService.updateCouMouldStatus(ids,status);
    }

    /**
     * 设置模块状态为完成状态
     * @param ids 模块ID串
     * @return Map<String,Object>
     */
    @RequestMapping("/updateMouldFinishStatus")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.COURSE,action = "设置模块状态为完成状态")
    public Map<String,Object> updateMouldFinishStatus(@RequestParam("ids") String ids){
        return couMouldService.updateMouldFinishStatus(ids);
    }

    /**
     * 初始化模块列表页面
     * @return 操作页面
     */
    @RequestMapping("/selectFindMouldList")
    public String selectFindMouldList(Model model,
                                      @RequestParam(value = "courseId",required = false,defaultValue = "0") BigDecimal courseId,
                                      @RequestParam(value = "selectType",required = false,defaultValue = "checkbox") String selectType,
                                      @RequestParam(value = "subjectIds",required = false,defaultValue = "") String subjectIds,
                                      @RequestParam(value = "type",required = false,defaultValue = "0") int type){
        model.addAttribute("selectType",selectType);
        model.addAttribute("subjectIds",subjectIds);
        model.addAttribute("type",type);
        model.addAttribute("courseId",courseId);
        return resultPage("/course/mould/select-mould-list");
    }

    /**
     * 加载模块数据
     * @param model
     * @param params
     * @param currentPage
     * @return
     */
    @RequestMapping("/ajaxSelectFindMouldList")
    public String ajaxSelectFindMouldList(Model model,
                                    HttpServletRequest request,
                                    @RequestBody Map<String,Object> params, @RequestParam("currentPage") int currentPage){
        params.put("sysUserId",loginUtil.getLoginUserId(request));
        Map<String,Object> dataMap = couMouldService.findCouMouldPage(params,currentPage,true,true,false);
        model.addAttribute("dataMap",dataMap);
        model.addAttribute("selectType",params.get("selectType"));
        return resultPage("/course/mould/select-mould-list-ajax");
    }

    /**
     * 多条件查询模块列表
     * @param params
     * @return
     */
    @RequestMapping("/selectMouldList")
    @ResponseBody
    public Map<String,Object> findMouldList(@RequestBody Map<String,Object> params){
        params.put("status",2);   //查询已发布的
        List<Map<String,Object>> couMouldList = couMouldService.findCouMouldList(params,false,false,false);
        return ResultUtil.SUCCESS(couMouldList);
    }
}
